import { parse, stringify } from 'zipson'
import { defineStore } from 'pinia'

export const useSearchStore = defineStore('searchStore', {
  state: () => ({
    historys: [] as any,
  }),
  getters: {
    /**
     * 搜索记录
     */
    historys: (state) => state.historys,
  },
  actions: {
    /**
     * 1. 新增的历史记录位于头部
     * 2. 不可出现重复的记录
     */
    addHistory(newHistory: any) {
      const isFindIndex = this.historys.findIndex((item: any) => item === newHistory)
      // 剔除旧数据
      if (isFindIndex !== -1) {
        this.historys.splice(isFindIndex, 1)
      }
      // 新增记录
      this.historys.unshift(newHistory)
    },
    /**
     * 删除指定数据
     */
    deleteHistory(index: any) {
      this.historys.splice(index, 1)
    },
    /**
     * 删除所有历史记录
     */
    deleteAllHistory() {
      this.historys = [] as any
    },
  },
  persist: {
    key: 'searchStore',
    storage: localStorage,
    serializer: {
      deserialize: parse,
      serialize: stringify,
    },
  },
})
